package com.zhulanhua.blogs.Utils;

import com.zhulanhua.blogs.domain.User;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.Map;

public class RequestContextUtils {
    public static HttpServletRequest getHttpServletRequest(){
        ServletRequestAttributes servletRequestAttributes =  (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();
        return request;
    }
    public static HttpServletResponse getHttpServletResponse(){
        ServletRequestAttributes servletRequestAttributes =  (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletResponse response = servletRequestAttributes.getResponse();
        return response;
    }
    public static User getUser(){
        Map<String, String> token = (Map) (getHttpServletRequest().getAttribute("token"));
        User user = new User();
        user.setUserId(Integer.valueOf(token.get("userId")));
        user.setUserRole(token.get("userRole"));
        user.setLoginTime(new Date(Long.valueOf(token.get("timeStamp"))));
        return user;
    }
}
